﻿using System.Collections.Generic;

namespace Macds.Core.Validation
{
    public abstract class ValidatorBase<T> 
    {
        protected T obj;
        protected abstract IEnumerable<IValidation> GetValidations(T obj);

        public ValidatorBase(T obj)
        {
            this.obj = obj;
        }

        public T Instance
        {
            get 
            {
                return this.obj; 
            }
        }

        protected void Validate()
        {
            foreach (var validation in GetValidations(obj))
            {
                if (validation.IsValid == false)
                {
                    throw validation.Exception;
                }
            }
        }

        public T GetValidData()
        {
            Validate();
            return this.Instance;
        }
    }
}
